const ava = require('ava')
const fsp = require('fs-promise')
const app = require('./helpers/app')

const config = require('./helpers/config')
const { success, error } = config

const {
    WORKFLOW,
    ITEM
} = config.modules

let id
ava.serial('[200] create workflow', async t => {
    const res = await app
        .post(`${WORKFLOW}`)
        .ava(t)
    id = res.body.data.workflow.id
})

ava.serial('[200] workflow list', async t => {
    const res = await app
        .get(`${WORKFLOW}`)
        .query({
            limit: 1,
            working: 1,
            step: 'preparation'
        })
        .ava(t)
})


let precheckId
ava.serial('[200] get precheck workflow', async t => {
    const res = await app
        .get(`${WORKFLOW}/precheck`)
        .ava(t)

    precheckId = res.body.data.workflow.id
})

ava.serial('[200] precheck reject workflow', async t => {
    const res = await app
        .put(`${WORKFLOW}/precheck/${precheckId}/reject`)
        .ava(t)
})

ava.serial('[200] precheck pass workflow', async t => {
    const res = await app
        .put(`${WORKFLOW}/precheck/${precheckId}/pass`)
        .ava(t)
})

let completeId
ava.serial('[200] complete get', async t => {
    const res = await app
        .get(`${WORKFLOW}/complete`)
        .ava(t)

    completeId = res.body.data.workflow.id
})

ava.serial('[200] complete pass', async t => {
    const res = await app
        .put(`${WORKFLOW}/complete/${completeId}`)
        .ava(t)
})

let postcheckId
ava.serial('[200] postcheck get', async t => {
    const res = await app
        .get(`${WORKFLOW}/postcheck`)
        .ava(t)

    postcheckId = res.body.data.workflow.id
})

ava.serial('[200] postcheck pass', async t => {
    const res = await app
        .put(`${WORKFLOW}/postcheck/${postcheckId}/pass`)
        .ava(t)
})
ava.serial('[200] postcheck reject', async t => {
    const res = await app
        .put(`${WORKFLOW}/postcheck/${postcheckId}/reject`)
        .ava(t)
})

ava.serial('[200] item snapshot', async t => {
    const res = await app
        .get(`${ITEM}/workflow/${id}/snapshot`)
        .ava(t)
})

ava.serial('[200] sophic aggregate', async t => {
    const res = await app
        .get(`${WORKFLOW}/aggregate`)
        .query({
            working: 1
        })
        .ava(t)
})